package com.tcode.utils;


import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
	
	//定义一个Properties对象
		private static Properties pro=null;
		//定义一个容器
		private static Map<String, Object> beans;
		
		static{
			try {
				//创建一个Properties对象
				pro=new Properties();
				//获得一个IO流，并读取bean.properties文件
//				InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
//				System.out.println(in+"========="+ToolsConfig.getIns());
				pro.load(ToolsConfig.getIns());
				//实例化容器
				beans=new HashMap<String,Object>();
				
				//获取对象的枚举型KEY值11111
				Enumeration<Object> keys =pro.keys();
				
				//遍历对象中的key,value
				while(keys.hasMoreElements()){
					//取出key
					String key =keys.nextElement().toString();
					//取出value，此为实现类的全限定名
					String beanpath=pro.getProperty(key);
					//利用反射实例化一个对象
					Object value = Class.forName(beanpath).newInstance();
					//填充进入容器
					beans.put(key, value);
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new ExceptionInInitializerError("初始化失败");
			}
		}
		//根据键，取出容器中的实例
		public static Object getBean(String beanName){
			return beans.get(beanName);
		}
}
